﻿using System;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using Microsoft.Win32;
using System.Runtime.Serialization.Formatters.Binary;

namespace dropbox_screen_shooter
{
    [Serializable]
    public sealed class Options : INotifyPropertyChanged
    {
        #region Singletone

        private static Options @default = new Options();

        private Options() { }

        public static Options Default 
        {
            get
            {
                return @default;
            }
            set { @default = value; }
        }

        #endregion

        #region Fields

        private const string run = @"Software\Microsoft\Windows\CurrentVersion\Run";

        private DirectoryInfo folder;
        private string folderString;
        private Types format;
        private int dbid;
        private bool autostart;
        private bool paste;

        #endregion

        #region Properties

        public DirectoryInfo Folder
        {
            get { return folder; }
            set 
            { 
                if (!value.Exists)
                    try
                    {
                        value.Create();
                    }
                    catch (IOException e)
                    {
                        throw e;
                    }
                folder = value; 
                
            }
        }

        public string FolderString
        {
            get { return folderString; }
            set 
            {
                if (!value.EndsWith(@"\"))
                    value += @"\";
                folderString = value;
                Folder = new DirectoryInfo(value);
                OnPropertyChanged(new PropertyChangedEventArgs("FolderString"));
            }
        }

        public Types Format
        {
            get { return format; }
            set 
            { 
                format = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Format"));
            }
        }

        public int DbID
        {
            get { return dbid; }
            set 
            {
                if (value > 0)
                {
                    dbid = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("DbID"));
                }
                else
                    dbid = 0;
            }
        }

        public bool Autostart
        {
            get { return autostart; }
            set 
            { 
                autostart = value;
                if (autostart)
                    SetAutoStart();
                else
                    UnsetAutoStart();
                OnPropertyChanged(new PropertyChangedEventArgs("Autostart"));
            }
        }

        public bool Paste
        {
            get { return paste; }
            set 
            {
                paste = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Paste"));
            }
        }

        #endregion

        #region Methods

        private void LoadDefault()
        {
            FolderString = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
            Format = Types.Jpeg;
            DbID = 0;
            Autostart = false;
            Paste = false;
        }

        public static void Load()
        {
            if (File.Exists(Properties.Settings.Default.OptionsFileName))
                using (Stream s = File.OpenRead(Properties.Settings.Default.OptionsFileName))
                {
                    BinaryFormatter bw = new BinaryFormatter();
                    Options.Default = (Options)bw.Deserialize(s);
                }
            else
                Default.LoadDefault();
        }

        public static void Save()
        {
            try
            {
                using (Stream s = File.Create(Properties.Settings.Default.OptionsFileName))
                {
                    BinaryFormatter bw = new BinaryFormatter();
                    bw.Serialize(s, Options.Default);
                }
            }
            catch (Exception e)
            {
                if (File.Exists(Properties.Settings.Default.OptionsFileName))
                    File.Delete(Properties.Settings.Default.OptionsFileName);
                throw e;
            }
        }

        private void SetAutoStart()
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(run);
            key.SetValue(Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().Location); 
        }

        private void UnsetAutoStart()
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(run);
            if (key.GetValue(Assembly.GetExecutingAssembly().GetName().Name) != null)
                key.DeleteValue(Assembly.GetExecutingAssembly().GetName().Name);
        }

        #endregion

        #region Events

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null) 
                PropertyChanged(this, e);
        }

        #endregion
    }
}
